const { valuesIn } = require("lodash")

class Left {
    static of (value){
        return new Left(value)
    }

    constructor (value){
        this._value = value
    }

    map (fn){
        return this
    }
}


class Right {
    static of (value){
        return new Right(value)
    }

    constructor (value){
        this._value = value
    }

    map (fn){
        return new Right(fn(this._value))
    }
}

function jsonParse (str){
    try{
        return Right.of(JSON.parse(str))
    }catch(e){
        return Left.of( {error : e.message} )
    }
}

// let r = jsonParse('{name : lucy}')
// console.log(r)

let r = jsonParse('{"name":"lucy"}').map(x => x.name.toUpperCase())
console.log(r)